package com.wuzu.ecommerce.utils;

import com.wuzu.ecommerce.exception.code.BaseResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一返回对象R
 *
 * @author jsm
 * @since 2023-03-26 17:25:26
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R implements Serializable {
    private static final long serialVersionUID = -11546952383036427L;
    private int code;
    private String message;
    private Object data;

    /**
     * 设置数据
     *
     * @param data 数据
     * @return R
     */
    public R setData(Object data) {
        this.data = data;
        return this;
    }

    public R(int code, String message) {
        this.code = code;
        this.message = message;
    }


    /**
     * 操作成功
     *
     * @return R
     */
    public static R ok() {
        return new R(BaseResponseCode.SUCCESS.getCode(), BaseResponseCode.SUCCESS.getMsg());
    }

    public static R ok(Object data) {
        return new R(BaseResponseCode.SUCCESS.getCode(), BaseResponseCode.SUCCESS.getMsg(), data);
    }

    /**
     * 认证授权失败。 包括密钥信息不正确；数字签名错误；授权已超时
     *
     * @return R
     */
    public static R fail() {
        return new R(BaseResponseCode.OPERATION_ERRO.getCode(), BaseResponseCode.OPERATION_ERRO.getMsg());
    }

    public static R fail(String msg) {
        return new R(BaseResponseCode.OPERATION_ERRO.getCode(), msg);
    }

    public static R fail(int code, String msg) {
        return new R(code, msg);
    }


    /**
     * 系统异常
     *
     * @return R
     */
    public static R exp() {
        return new R(BaseResponseCode.SYSTEM_BUSY.getCode(), BaseResponseCode.SYSTEM_BUSY.getMsg());
    }

    /**
     * 自定义返回结果
     *
     * @return R
     */
    public static R customer(int code, String msg, Object data) {
        return new R(code, msg, data);
    }
}


